package com.ruoyi.web.controller.shopping.shoppingCart;

import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.ShoppingCart;
import com.ruoyi.system.domain.ShoppingCartDto;
import com.ruoyi.system.service.ProductService;
import com.ruoyi.system.service.ShoppingCartService;
import com.ruoyi.system.view.common.R;
import com.ruoyi.web.controller.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/xiaomi/v2/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ProductService productService;

    @GetMapping
    public R<List<ShoppingCartDto>> getShoppingCart(HttpServletRequest request){
        String userId = TokenUtil.userIdTokenCheck(request);
//        System.out.println(userId);
        List<ShoppingCartDto> byUserId = shoppingCartService.getByUserId(Integer.valueOf(userId));
        for (ShoppingCartDto shoppingCartDto : byUserId) {
            Integer productId = shoppingCartDto.getProductId();
            Product all = productService.getAll(null, productId, null);
            shoppingCartDto.setProductSellingPrice(all.getProductSellingPrice());
            shoppingCartDto.setProductPicture(all.getProductPicture());
            shoppingCartDto.setProductName(all.getProductName());
        }
        return R.success(byUserId);
    }

    @PostMapping
    public R<String> addShoppingCart(HttpServletRequest request,@RequestBody ShoppingCart shoppingCart){
        System.out.println("有没有:"+shoppingCart.getProductId());
        String userId = TokenUtil.userIdTokenCheck(request);
        //查询改商品是不是已经添加进去购物车了，如果没有那么新增，如果有那么num+1
        ShoppingCart byUidPid = shoppingCartService.getByUidPid(Integer.valueOf(userId), shoppingCart.getProductId());
        if (byUidPid == null){
            //新增
            shoppingCart.setNum(1);
            shoppingCart.setUser_id(Integer.valueOf(userId));
            shoppingCartService.insert(shoppingCart);
        }else {
            //num+1
            shoppingCartService.update(byUidPid.getNum()+1,byUidPid.getShoppingCartId());
        }
        return R.success("添加购物车成功。");
    }

    @GetMapping("/product/count")
    public R<String> countShoppingCart(HttpServletRequest request){
        String s = TokenUtil.userIdTokenCheck(request);
        List<ShoppingCartDto> byUserId = shoppingCartService.getByUserId(Integer.valueOf(s));
        int size = byUserId.size();
        return R.success(String.valueOf(size));
    }

    @PutMapping("/product/num")
    public R<String> numUpdate(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.update(shoppingCart.getNum(),shoppingCart.getShoppingCartId());
        return R.success("修改成功");
    }

    @DeleteMapping("/{shoppingCartId}")
    public R<String> deleteShoppingCart(@PathVariable(value = "shoppingCartId") Integer shoppingCartId){
        shoppingCartService.deleteByShoppingCartId(shoppingCartId);
        return R.success("删除成功。");
    }
}
